import unittest
import ddt
from common.base import get_driver
from page.address_list_page import AddressListPage
from page.login_page import LoginPage
from common.data_operation import DataOperation
from page.index_page import IndexPage
import time
from selenium.webdriver.common.by import By

data=DataOperation("address_list.csv")
user_data=data.get_data_to_dict()

@ddt.ddt
class LoginTestCase(unittest.TestCase):
    user_data = user_data

    @classmethod
    def setUpClass(cls) -> None:
        # 创建浏览器对象
        driver = get_driver()
        #请求网页
        driver.get(LoginPage.login_url)

        cls.login = LoginPage(driver)
        cls.index = IndexPage(driver)
        cls.address=AddressListPage(driver)

    @ddt.data(*user_data)
    def test_01_login(self,content):
        username=content['username']
        password=content['password']
        #登录
        self.login.input_username(username)
        self.login.input_password(password)
        self.login.input_submit()
        #点击收货地址
        self.address.clicl_theFrom()
        self.address.choose_pro('//select[@id="selProvinces_1"]/option[@value="24"]')
        time.sleep(2)

        #新增
        self.address.choose_cuntry('//select[@id="selCities_1"]/option[@value="271"]')
        time.sleep(2)
        self.address.choose_district('//select[@id="selDistricts_1"]/option[@value="2715"]')
        time.sleep(2)
        self.address.input_name("consignee_1",'李四')
        self.address.input_emaill("email_1",'51111@qq.com')
        self.address.input_addr("address_1",'四川省温江区羊马')
        self.address.input_tel('tel_1','13525252525')
        self.address.click_submit('bnt_blue_2')
        time.sleep(4)

        #断言
        """
        预期结果和实际结果对比
        新增用户名是否存在
        """
        # 定义预期结果
        name='李四'
        # print(name)
        username = self.address.get_attribute((By.XPATH, "//input[@id='consignee_1']"),"value")
        # print(username)
        # print(actual_username)
        self.assertEqual(name,username, msg='新增失败')

        #断言查找信息
        # 定义预期结果
        name = '李四'
        # print(name)
        username = self.address.get_attribute((By.XPATH, "//input[@id='consignee_1']"), "value")
        # print(username)
        # print(actual_username)
        self.assertEqual(name, username, msg='查找失败')

        #断言修改信息
        self.address.change_addr()
        self.address.delete_addr()
        self.address.add_name('王二')
        self.address.click_submit1()

        name='王二'
        username = self.address.get_attribute((By.XPATH, "//input[@id='consignee_0']"), "value")
        # print(username)
        # print(actual_username)
        self.assertEqual(name, username, msg='修改失败')



    @classmethod
    def tearDownClass(cls) -> None:
        cls.address.quit()



if __name__ == '__main__':
    unittest.main(verbosity=2)





